package com.group2.view;
import static model.netBase.IP;
import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.event.*;
import java.awt.CardLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.LineBorder;
import java.awt.Color;
import java.io.*;
import java.net.Socket;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.ArrayList;
import javax.swing.border.SoftBevelBorder;
import javax.swing.border.BevelBorder;
import model.*;
public class BankMainFrame_Student extends JFrame {

	private JPanel contentPane;
	private String studentId;
	private Account MyAccount=null;
	private Timer timer;
	CardLayout cardlayout=new CardLayout(0, 0);

	/**
	 * Launch the application.
	 */
	//拉取账户信息
	public void refresh() throws IOException, ClassNotFoundException {
		System.out.println("开始传输");
		//拉取选课队列
		Socket socket = new Socket(IP(), 2000);//此处主机地址请自行修改
		//设置超时时间
		socket.setSoTimeout(3000);
		//构建IO
		OutputStream os = socket.getOutputStream();
		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os));
		bw.write("61" + '\n'+studentId+'\n');
		bw.flush();
		//从服务端输入到本地
		InputStream is = socket.getInputStream();
		ObjectInputStream in = new ObjectInputStream(is);
		Object obj = in.readObject();
		System.out.println("传输结束");
		MyAccount= (Account) obj;
		//输出输入流关闭
		is.close();
		os.close();
		socket.close();
	}
	/**
	 * Create the frame.
	 */
	public BankMainFrame_Student(String id, Timer time) throws IOException, ClassNotFoundException {
		time.start();
		this.addMouseMotionListener(new MouseMotionListener() {@Override public void mouseDragged(MouseEvent e) {}@Override public void mouseMoved(MouseEvent e) {time.stop();}});
		this.addMouseListener(new MouseListener() {@Override public void mouseClicked(MouseEvent e) {}@Override public void mousePressed(MouseEvent e) {}@Override public void mouseReleased(MouseEvent e) {}@Override public void mouseEntered(MouseEvent e) {time.start();}@Override public void mouseExited(MouseEvent e) {time.start();}});
		studentId=id;
		timer=time;
		setTitle("一卡通管理系统");
		setBounds(100, 100, 393, 385);
		this.setLocationRelativeTo(null);
		this.setResizable(false);
		JMenuBar menuBar = new JMenuBar();
		setJMenuBar(menuBar);
		//拉取我的银行账户信息
		refresh();

		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		JPanel panel = new JPanel();
		panel.setBorder(new SoftBevelBorder(BevelBorder.LOWERED, null, null, null, null));
		JPanel panel_1 = new JPanel();
		GroupLayout gl_contentPane = new GroupLayout(contentPane);
		gl_contentPane.setHorizontalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING)
						.addComponent(panel_1, Alignment.LEADING, 0, 0, Short.MAX_VALUE)
						.addComponent(panel, Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 367, GroupLayout.PREFERRED_SIZE))
					.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
		);
		gl_contentPane.setVerticalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addComponent(panel, GroupLayout.PREFERRED_SIZE, 54, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(panel_1, GroupLayout.DEFAULT_SIZE, 245, Short.MAX_VALUE))
		);
		
		JLabel lblNewLabel = new JLabel("当前一卡通余额："+MyAccount.getAccountBalance());

		JLabel lblNewLabel_1 = new JLabel("您有"+MyAccount.getBillsNum()+"笔未缴纳的费用");
		GroupLayout gl_panel = new GroupLayout(panel);
		gl_panel.setHorizontalGroup(
			gl_panel.createParallelGroup(Alignment.TRAILING)
				.addGroup(gl_panel.createSequentialGroup()
					.addContainerGap(94, Short.MAX_VALUE)
					.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_panel.createSequentialGroup()
							.addComponent(lblNewLabel, GroupLayout.PREFERRED_SIZE, 178, GroupLayout.PREFERRED_SIZE)
							.addGap(89))
						.addGroup(Alignment.TRAILING, gl_panel.createSequentialGroup()
							.addComponent(lblNewLabel_1)
							.addGap(121))))
		);
		gl_panel.setVerticalGroup(
			gl_panel.createParallelGroup(Alignment.TRAILING)
				.addGroup(gl_panel.createSequentialGroup()
					.addGap(5)
					.addComponent(lblNewLabel, GroupLayout.DEFAULT_SIZE, 12, Short.MAX_VALUE)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(lblNewLabel_1)
					.addContainerGap())
		);
		panel.setLayout(gl_panel);
		panel_1.setLayout(cardlayout);
		contentPane.setLayout(gl_contentPane);
		
		BankAccountFrame bankAccountFrame=new BankAccountFrame(studentId,MyAccount);
		bankAccountFrame.setBorder(new SoftBevelBorder(BevelBorder.LOWERED, null, null, null, null));
		panel_1.add(bankAccountFrame,"我的账单");
		
		BankTopUpFrame bankTopUpFrame=new BankTopUpFrame(studentId,MyAccount,lblNewLabel,lblNewLabel_1);
		panel_1.add(bankTopUpFrame,"卡片充值");
		
		BankPayFrame bankPayFrame=new BankPayFrame(studentId,MyAccount);
		panel_1.add(bankPayFrame,"缴纳费用");

		JButton btnNewButton = new JButton("我的账单");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				cardlayout.show(panel_1,"我的账单");
			}
		});
		menuBar.add(btnNewButton);
		
		JButton btnNewButton_1 = new JButton("缴纳费用");
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				cardlayout.show(panel_1,"缴纳费用");
			}
		});
		menuBar.add(btnNewButton_1);
		
		JButton btnNewButton_2 = new JButton("卡片充值");
		btnNewButton_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {cardlayout.show(panel_1,"卡片充值");}
		});
		menuBar.add(btnNewButton_2);
	}

}
